package com.roberto.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.roberto.entities.UFO;
import com.roberto.simulation.GameLogic;
import com.roberto.simulation.IRenderer;
import com.roberto.simulation.Renderer;

public class GameLoop extends AbstractScreen {
    /** the simulation **/
    private final GameLogic gameLogic;
    /** the renderer **/
    private final IRenderer renderer;

    public GameLoop() {
        renderer = new Renderer();
        gameLogic = new GameLogic();
    }

    @Override
    public void dispose() {
        renderer.dispose();
    }

    @Override
    public boolean isDone() {
        if (gameLogic.player.lives == 0 && Gdx.input.isKeyPressed(Keys.ENTER) || (Gdx.input.isKeyPressed(Keys.ESCAPE))) {
            UFO.stopSound();
            return true;
        }
        return false;
    }

    @Override
    public void draw(float delta) {
        renderer.render(gameLogic, delta);
    }

    @Override
    public void update(float delta) {
        gameLogic.update(delta);

        if (Gdx.input.isKeyPressed(Keys.DPAD_LEFT) || Gdx.input.isKeyPressed(Keys.A)) {
            gameLogic.moveShipLeft(delta);
        }

        if (Gdx.input.isKeyPressed(Keys.DPAD_RIGHT) || Gdx.input.isKeyPressed(Keys.D)) {
            gameLogic.moveShipRight(delta);
        }

        if (Gdx.input.isTouched() || Gdx.input.isKeyPressed(Keys.SPACE)) {
            gameLogic.shot();
        }

    }

}
